package drds.server.route.parser.util;

public final class Pair<Key, Value> {

	private final Key key;
	private final Value value;

	public Pair(Key key, Value value) {
		this.key = key;
		this.value = value;
	}

	public Key getKey() {
		return key;
	}

	public Value getValue() {
		return value;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("(").append(key).append(", ").append(value).append(")");
		return sb.toString();
	}

	private static final int HASH_CONST = 37;

	@Override
	public int hashCode() {
		int hash = 17;
		if (key == null) {
			hash += HASH_CONST;
		} else {
			hash = hash << 5 + hash << 1 + hash + key.hashCode();
		}
		if (value == null) {
			hash += HASH_CONST;
		} else {
			hash = hash << 5 + hash << 1 + hash + value.hashCode();
		}
		return hash;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public boolean equals(Object o) {
		if (this == o) {
			return true;
		}
		if (!(o instanceof Pair)) {
			return false;
		}
		Pair that = (Pair) o;
		return isEquals(this.key, that.key) && isEquals(this.value, that.value);
	}

	private boolean isEquals(Object o1, Object o2) {
		if (o1 == o2) {
			return true;
		}
		if (o1 == null) {
			return o2 == null;
		}
		return o1.equals(o2);
	}

}